$(document).ready(function() {
    $.ajax({
        url: "http://jx.xuzhixiang.top/ap/api/cart-list.php",
        type: "get",
        data: {
            id: 69007,
        },
        success: function(res) {
            console.log(res);

            let cart = document.querySelector(".cartList");
            let element = "";
            for (var i = 0; i < res.data.length; i++) {
                element += `
               <li data-id="${res.data[i].pid}">
               <input type="checkbox" class="ck">
			    	<img src="${res.data[i].pimg}">
				       <p>${res.data[i].pname}</p>

                <span class="minus">-</span>
                <input type="text" value="${res.data[i].pnum}" class="num">
                <span class="plus">+</span>
                <span class="perPrice">${res.data[i].pprice}元</span>
                <span class="perTotalPrice">${
                  res.data[i].pprice * res.data[i].pnum
                }</span>
                <input type="button" value="删除" class="delBtn" />
                </li>
			    `;

                cart.innerHTML = element;


                let oMinus = document.querySelector(".minus");
                let oPlus = document.querySelector(".plus");
                let oNum = document.querySelector(".num");
            }
            //  减号

            $(".minus").click(function() {
                let pid = $(this).parent().attr("data-id");
                // oNum.value--;
                let num = $(this).next().val();
                console.log(num);
                if (num == 1) {
                    return false;
                }
                num--;
                $(this).next().val(num)
                $.ajax({
                    url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
                    type: "get",
                    data: {
                        uid: 69007,
                        pid: pid,
                        pnum: num,
                    },
                    success: function(res) {
                        console.log(res);
                    },
                });
                //更改总价格
                $(this).parent().find(".perTotalPrice").html($(this).parent().find(".num").val() * parseFloat($(this).parent().find(".perPrice").text()))
            });

            //  加号
            $(".plus").click(function() {
                let pid = $(this).parent().attr("data-id");
                // oNum.value++;
                let num = $(this).prev().val();
                console.log(num);

                num++;
                $(this).prev().val(num)
                $.ajax({
                    url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
                    type: "get",
                    data: {
                        uid: 69007,
                        pid: pid,
                        pnum: num,
                    },
                    success: function(res) {
                        console.log(res);
                    },
                });



                $(this).parent().find(".perTotalPrice").html($(this).parent().find(".num").val() * parseFloat($(this).parent().find(".perPrice").text()))
            })



            // 删除
            $(".delBtn").click(function() {
                let pid = $(this).parent().attr("data-id");
                $(this).parent().remove();
                $.ajax({
                    url: "http://jx.xuzhixiang.top/ap/api/cart-delete.php",
                    type: "get",
                    data: {
                        uid: 69007,
                        pid: pid,
                    },
                    success: function(res) {
                        console.log(res);
                    },
                });
            });
        },
    });
});